package _04_queue.my_implements;

import _00_my.exception.TechnicalLimitationException;
import _04_queue.AbstractQueue;
import _05_stack.my_implements.ArrayStack;

import java.util.Iterator;

/**
 * @author: mornd
 * @dateTime: 2023/6/7 - 14:40
 * 使用两个栈来模拟队列(简易版)
 */
public class MyQueue<E> extends AbstractQueue<E> {
    ArrayStack<E> head = new ArrayStack<>(100);
    ArrayStack<E> tail = new ArrayStack<>(100);

    public static void main(String[] args) {
        MyQueue<Integer> queue = new MyQueue<>();
        queue.offer(1);
        queue.offer(2);
        System.out.println(queue.poll());
        System.out.println(queue.poll());
    }

    /*
        a b

        head        tail
        b a          c d


        a            b
                     321


      123

     */
    @Override
    public boolean offer(E e) {
        return tail.push(e);
    }

    private void swap() {
        while (!tail.isEmpty()) {
            head.push(tail.pop());
        }
    }

    @Override
    public E poll() {
        if(head.isEmpty()) {
            swap();
        }
        return head.pop();
    }

    @Override
    public E peek() {
        if(head.isEmpty()) {
            swap();
        }
        return head.peek();
    }

    @Override
    public boolean isEmpty() {
        return head.isEmpty() && tail.isEmpty();
    }

    @Override
    public boolean isFull() {
        throw new TechnicalLimitationException("懒得写");
    }

    @Override
    public Iterator<E> iterator() {
        throw new TechnicalLimitationException("懒的写");
    }

    @Override
    public void clear() {
        throw new TechnicalLimitationException();
    }
}
